package globals;

import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

/**
 * @author Ben Elliott
 *
 */
public class InputManager implements MouseMotionListener, KeyListener, MouseListener {	
	public int mouseX, mouseY, deltaX, deltaY;
	public boolean leftMouseDown, rightMouseDown, mouseClicked;
	
	public boolean[] key_state_up = new boolean[256];
	public boolean[] key_state_down = new boolean[256];
	
	private String buffer;
	private boolean backSpace;
	
	public InputManager() {
		// setup?
		buffer = "";
	}
	
	public Point getMouseLocation() {
		return MouseInfo.getPointerInfo().getLocation();
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		deltaX += e.getX() - mouseX;
		deltaY += e.getY() - mouseY;
		mouseX = e.getX();
		mouseY = e.getY();
	}
	@Override
	public void mouseMoved(MouseEvent e) {
		mouseX = e.getX();
		mouseY = e.getY();
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if( e.getKeyCode() >= 0 && e.getKeyCode() < 256 ) {
			key_state_down[e.getKeyCode()] = true;
			key_state_up[e.getKeyCode()] = false;
		}
	}
	
	@Override
	public void keyReleased(KeyEvent e) {
		if( e.getKeyCode() >= 0 && e.getKeyCode() < 256 ) {
			key_state_up[e.getKeyCode()] = true;
			key_state_down[e.getKeyCode()] = false;
		}
	}
	
	public boolean isKeyDown( int key ) {
		return key_state_down[key];
	}
	
	public boolean isKeyReleased( int key ) {
		return key_state_up[key];
	}
	
	@Override
	public void keyTyped(KeyEvent e) {
		if (e.getKeyChar() == '\b') {
			backSpace = true;
		} else {
			buffer = buffer.concat( ((Character)e.getKeyChar()).toString() );
		}
	}
	
	public boolean isBackSpace() {
		if (backSpace) {
			backSpace = false;
			return true;
		}
		
		return false;
	}
	
	public String isKeyTyped() {
		return buffer;
	}
	
	public void clear() {
		//clear out the key up states
		key_state_up = new boolean[256];
		
		// If nothing used the click, remove it
		mouseClicked = false;
		
		// Remove keyTyped
		buffer = "";
		
		// Reset values
		deltaX = deltaY = 0;
	}

	/** Consumes the mouse click.*/
	public boolean isMouseClicked() {
		if (mouseClicked) {
			mouseClicked = false;
			return true;
		}
		
		return false;
	}
	/** Does not consume the mouse click.*/
	public boolean isMouseClickedCheck() {
		if (mouseClicked) {
			return true;
		}
		
		return false;
	}
	
	@Override
	public void mouseClicked(MouseEvent arg0) {
		mouseClicked = true;
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// Left click
		if (arg0.getButton() == MouseEvent.BUTTON1)
			leftMouseDown = true;
		// Right click
		if (arg0.getButton() == MouseEvent.BUTTON3)
			rightMouseDown = true;
				
		// Wheel click
		//if (arg0.getButton() == MouseEvent.BUTTON2)
			
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// Left click
		if (arg0.getButton() == MouseEvent.BUTTON1)
			leftMouseDown = false;
		// Right click
		if (arg0.getButton() == MouseEvent.BUTTON3)
			rightMouseDown = false;
	}
}
